<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_custom_model;
use cmf\lib\Plugin;
use plugins\qbt_custom_model\service\ImportService;
use think\Db;
use think\Config;
//加载函数
include str_replace("\\",DS,__DIR__).DS."common.php";
class QbtCustomModelPlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtCustomModel',
        'title'       => '千佰特自定义模型',
        'description' => '快速定制后台功能',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com'
    ];

    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}model` (
            `model_id`  SMALLINT(5) NOT NULL AUTO_INCREMENT,
            `menu_id`  INT(10) NOT NULL DEFAULT 0 COMMENT '菜单id',
            `model_menu`  INT(10) NOT NULL DEFAULT 0 COMMENT '菜单位置',
            `model_name`  varchar(30) NULL DEFAULT '' COMMENT '名称' ,
            `model_app`  varchar(30) NULL DEFAULT '' COMMENT '模块' ,
            `model_controller`  varchar(30) NULL DEFAULT '' COMMENT '控制器' ,
            `model_tabname`  varchar(30) NULL DEFAULT '' COMMENT '数据表' ,
            `model_description`  varchar(150) NULL DEFAULT '' COMMENT '描述' ,
            `model_status`  TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态' ,
            `addtime`  int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '添加时间' ,
            `model_ico`  varchar(15) NOT NULL DEFAULT '' COMMENT '模型小图标' ,
            `model_type`  TINYINT(2) NOT NULL DEFAULT 0 COMMENT '属性，0普通模型1内置模型' ,
            `distinguish` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '分类，1分类模型2信息模型',
            `is_menu` tinyint(1) unsigned DEFAULT '0' COMMENT '生成菜单 0 否 1 是',
             `is_default_field` tinyint(1) unsigned DEFAULT '1' COMMENT '是否添加默认字段',
            PRIMARY KEY (`model_id`),
            KEY(`model_name`),
            UNIQUE KEY(`model_tabname`)
            )ENGINE=InnoDB DEFAULT CHARSET=UTF8
        ");

        Db::query("
        CREATE TABLE IF NOT EXISTS `{$config['prefix']}model_field` (
          `field_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '字段id',
          `model_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '模型id',
          `field_name` varchar(60) NOT NULL COMMENT '字段名称',
          `field_nikename` varchar(60) NOT NULL COMMENT '字段别名',
          `field_type` varchar(30) NOT NULL COMMENT '字段类型',
          `field_type2` varchar(30) NOT NULL COMMENT '字段属性',
          `field_length` int(10) unsigned NOT NULL COMMENT '字段长度',
          `field_digits` int(10) unsigned NOT NULL COMMENT '小数位数',
          `field_default` varchar(500) NOT NULL COMMENT '默认值',
          `field_choose` text NOT NULL COMMENT '可选项',
          `field_tips` varchar(300) NOT NULL COMMENT '字段提示',
          `field_min_length` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最小值',
          `field_max_length` int(10) unsigned NOT NULL COMMENT '最大值',
          `field_status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态：1启用0禁用',
          `field_is_required` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1必填0非必填',
          `field_regex` varchar(300) NOT NULL COMMENT '匹配正则',
          `field_sort`  smallint(6) NOT NULL COMMENT '字段排序',
          `field_outreach`  varchar(100) NOT NULL DEFAULT '' COMMENT '外联字段',
          `field_is_default` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '是否为默认字段',
          `field_add_show` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '添加展示',
          `field_is_index` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '列表页展示',
          `field_file_type` char(10) NOT NULL DEFAULT '' COMMENT '文件类型',
          `field_edit_show` tinyint(1) unsigned DEFAULT NULL COMMENT '编辑时展示',
          `field_add_edit_show_many_column` tinyint(1) unsigned DEFAULT '0',
          PRIMARY KEY (`field_id`),
          KEY `model_id`(`model_id`),
          KEY `field_name`(`field_name`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        ");

        //开始内置模型
        $filename = WEB_ROOT . 'plugins/' .'qbt_custom_model/data/default/model.sql';
        if(file_exists($filename)){
            ImportService::import_sql($filename);
        }
        return true;

    }

    // 插件卸载
    public function uninstall()
    {
       $config = Config::get('database');
        #nsh 20190107 修改
       $data = Db::name('model')->where('model_type','<>','1')->select()->toArray();
       if(!empty($data)){
         return   ['msg'=>'存在非内置模型,无法卸载！请先删除非内置模型'];
       }
        Db::query("DROP TABLE `{$config['prefix']}model`");
        Db::query("DROP TABLE `{$config['prefix']}model_field`");
        Db::query("DROP TABLE `{$config['prefix']}article`");
        Db::query("DROP TABLE `{$config['prefix']}article_category`");
        return true;
    }


}